package services.socialuser.util;

import play.Logger;
import models.User;
import securesocial.provider.AuthenticationMethod;
import securesocial.provider.ProviderType;
import securesocial.provider.SocialUser;
import securesocial.provider.UserId;

public class SocialUserAdaptor implements UserAdapter
{

	private SocialUser user;

	public SocialUserAdaptor(SocialUser user) 
	{
		this.user = user;
	}

	public User getUser() 
	{
		User u = User.findById(user.id.provider + DELIMITER + user.id.id);
		
		if (u == null)
		{	
			u = new User();
		}
		
		
		u.stringID = user.id.provider + DELIMITER + user.id.id;
		
		u.provider = user.id.provider.toString();
		u.accessToken = user.accessToken;
		
		u.authMethod = user.authMethod.toString();
		u.lastAccess = user.lastAccess;
		u.avatarUrl = user.avatarUrl;
		u.isEmailVerified = user.isEmailVerified;
		u.displayName = user.displayName;
		u.email = user.email;
		u.password = user.password;
		u.secret = user.secret;
		u.token = user.token;
		
		return u;
		
	}

	@Override
	public void printUser() {
		
		if(user != null)
		{
			if (user.id != null)
			{
				Logger.info("SocialUserAdaptor: " + 
						"\n\tuser.id:\t" + user.id.id +
						"\n\tuser.provider:\t" + user.id.provider.toString() +
						"\n\tuser.authMethod:\t" + user.authMethod.toString() + 
						"\n\tuser.lastAccess:\t"+ user.lastAccess +
						"\n\tuser.avatarUrl:\t" + user.avatarUrl + 
						"\n\tuser.isEmailVerified\t" + user.isEmailVerified + 
						"\n\tuser.displayName:\t" + user.displayName +
						"\n\tuser.email:\t" + user.email +
						"\n\tuser.password:\t" + user.password +
						"\n\tuser.secret:\t" + user.secret  +
						"\n\tuser.token:\t" + user.token	);
			}
			else
			{
				Logger.info("SocialUserAdaptor: user.id is NULL");
			}
		}
		else
		{
			Logger.info("SocialUserAdaptor: user is NULL");
		}
		
	}

}
